// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov  Date: 2010-4-9 13:51:04
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   FreemarkerRender.java

package com.freework.buildercode.freemarker;

import freemarker.cache.FileTemplateLoader;
import freemarker.template.*;
import java.io.*;
import java.util.Locale;
import java.util.Map;

// Referenced classes of package test.freemarker:
//            Render

public class FreemarkerRender
    implements Render
{

	String charsetName="";
    public FreemarkerRender(String charsetName)
    {
    	this.charsetName=charsetName;
        initialize();
    }

    public void initialize()
    {
        try
        {
            if(templateconfig == null)
            {
                templateconfig = new Configuration();
                templateconfig.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
                templateconfig.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER);
                templateconfig.setTemplateLoader(new FileTemplateLoader());
                templateconfig.setTemplateUpdateDelay(1200);
                templateconfig.setDefaultEncoding(this.charsetName);
                templateconfig.setLocale(new Locale("zh_CN"));
                templateconfig.setNumberFormat("0.##########");
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void render(Object target, String template, String outpath)
        throws Exception
    {
        StringWriter writer = new StringWriter();
        Template tl = templateconfig.getTemplate(template);
        SimpleHash root = new SimpleHash();
        root.put("entity", target);
        tl.process(root, writer);
        FileOutputStream fos = new FileOutputStream(outpath);
        
        PrintWriter pwriter = new PrintWriter(new OutputStreamWriter (fos,this.charsetName));
        pwriter.write(writer.toString());
        pwriter.close();
        fos.close();
    }
	    public void render(Object target, String template, OutputStream fos)
	    throws Exception
	{
	    StringWriter writer = new StringWriter();
	    Template tl = templateconfig.getTemplate(template);
	    SimpleHash root = new SimpleHash();
	    root.put("entity", target);
	    tl.process(root, writer);
	    PrintWriter pwriter = new PrintWriter(new OutputStreamWriter (fos,this.charsetName));
	    pwriter.write(writer.toString());
	    pwriter.close();
	    fos.close();
	}

    private Configuration templateconfig;

}